package com.wits.comet.chat.service;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.wits.comet.chat.entity.ChatMember;
import com.wits.platform.service.account.ShiroUser;
/**
 * 为同一用户不能多处登录准备的当前登录用户。 
 */
public class CurrentLoginUser {
	
	/**
	 * 
	 * inKey:  用户Id
	 * 
	 */
	private static final Map<String,ChatMember> userMap=new HashMap<String,ChatMember>();
	
	private static CurrentLoginUser currentLoginUser=new CurrentLoginUser();
	
	private CurrentLoginUser(){
		
	}
	
	public synchronized static CurrentLoginUser getInstance() {
		return currentLoginUser;
	}

	
	/**
	 * 
	 * @param memberId 用户id
	 * @param member
	 */
	public synchronized void addMember(String memberId,ChatMember member){
		try{
			if(memberId==null){
				return;
			}
			userMap.put(memberId,member);
		}finally{
		}
	}
	
	public synchronized void addMember(ShiroUser user){
		try{
			if(user==null){
				return;
			}
			ChatMember member = new ChatMember();
			member.setMemberId(user.getId());
			member.setMemberName(user.getName());
			member.setMemberType(user.getUserType());
			userMap.put(member.getMemberId(),member);
		}finally{
		}
	}
	/**
	 *  
	 * @param memberId 用户id
	 * @return
	 */
	public synchronized ChatMember getMember(String memberId){
		try{
			if(memberId==null){
				return null;
			}
			return userMap.get(memberId);
		}finally{
		}

	}
	
	public synchronized void removeMember(String memberId){
		try{
			if(memberId==null){
				return;
			}
			userMap.remove(memberId);
		}finally{
		}

		
	}
	

	
    public synchronized Set<String> allKeys() {
        try { 
        	return userMap.keySet(); 
        }finally { 
        }
    }
    
	public Collection<ChatMember> allValues() {
		try {
			return userMap.values();
		} finally {
		}

	}
    
	public synchronized void clear() {
		try {
			userMap.clear();
		} finally {
		}
	}
	
	
	
	
	

}
